package com.yh.workbill.ui.search.mvp;

import android.content.ComponentName;
import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class Search_Model {

    public interface SearchCallBack extends IBaseModel.Fail {
        void getData(List<WorkBill> data);
    }


    public void getData(String companyname, String year, String month, int pageindex, int pagenumber, SearchCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().getSearch(singleUserInfo.getUuid(), companyname, year, month, pageindex, pagenumber, new BaseCallback<List<WorkBill>>() {
                @Override
                public void accessSuccess(int code, String msg, List<WorkBill> data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.getData(data);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                }
            });
        });
    }
}
